/*
 * ImportPathDialog.java
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui.dialogs;

import org.fpdev.apps.rtemaster.gui.*;
import org.fpdev.core.transit.Route;
import org.fpdev.core.transit.SubRoute;
import org.fpdev.apps.rtemaster.RouteMaster;
import java.awt.event.*;
import java.util.Iterator;
import javax.swing.*;
import javax.swing.border.*;
import org.fpdev.core.transit.Provider;
import org.fpdev.core.transit.Routes;

public class ImportPathDialog extends JDialog implements ActionListener {

  private Routes routes_;
  private JButton okBtn_,  cancelBtn_;
  private boolean cancelled_;
  private JComboBox provsCBox_,  routesCBox_,  subsCBox_;
  private JCheckBox reverseCB_;
  private Provider[] provLookup_;
  private Route[] routeLookup_;

  public ImportPathDialog(RouteMaster cv, int selRteIndex) {
    super(cv.getGUI(), "Copy Path", true);

    routes_ = cv.getEngine().getRoutes();
    cancelled_ = false;

    provsCBox_ = new JComboBox();
    routesCBox_ = new JComboBox();
    subsCBox_ = new JComboBox();

    Provider curProv = cv.getGUI().getRouteSelectionPanel().getSelectedProvider();

    Iterator<Provider> provs = routes_.getProviders().getIterator();
    provLookup_ = new Provider[routes_.getProviders().providerCount()];
    int i = 0, selProv = 0;
    while (provs.hasNext()) {
      Provider prov = provs.next();
      provLookup_[i] = prov;
      provsCBox_.addItem(prov.getName());
      if (prov == curProv) {
        selProv = i;
      }
      i++;
    }
    provsCBox_.setSelectedIndex(selProv);
    provsCBox_.addItemListener(new ItemListener() {

      @Override
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          updateRoutes(provLookup_[provsCBox_.getSelectedIndex()], 0);
        }
      }
    });

    updateRoutes(curProv, selRteIndex);
    routesCBox_.addItemListener(new ItemListener() {

      @Override
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          updateSubs();
        }
      }
    });

    reverseCB_ = new JCheckBox("Reverse path?");

    okBtn_ = new JButton("OK");
    okBtn_.addActionListener(this);

    cancelBtn_ = new JButton("Cancel");
    cancelBtn_.addActionListener(this);

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));

    topPanel.add(new JLabel("All Providers:"));
    provsCBox_.setAlignmentX(LEFT_ALIGNMENT);
    topPanel.add(provsCBox_);

    topPanel.add(Box.createVerticalStrut(6));

    topPanel.add(new JLabel("All Routes:"));
    routesCBox_.setAlignmentX(LEFT_ALIGNMENT);
    topPanel.add(routesCBox_);

    topPanel.add(Box.createVerticalStrut(6));

    topPanel.add(new JLabel("Member Subroutes:"));
    subsCBox_.setAlignmentX(LEFT_ALIGNMENT);
    topPanel.add(subsCBox_);

    topPanel.add(Box.createVerticalStrut(6));
    topPanel.add(reverseCB_);

    JPanel btnRow = new JPanel();
    btnRow.setLayout(new BoxLayout(btnRow, BoxLayout.X_AXIS));
    btnRow.add(okBtn_);
    btnRow.add(Box.createHorizontalStrut(5));
    btnRow.add(cancelBtn_);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    topPanel.setAlignmentX(CENTER_ALIGNMENT);
    mainPanel.add(topPanel);
    mainPanel.add(Box.createVerticalStrut(6));
    btnRow.setAlignmentX(CENTER_ALIGNMENT);
    mainPanel.add(btnRow);

    mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(mainPanel);

    this.pack();
    this.setLocation((cv.getGUI().getWidth() / 2) - getWidth() / 2 + cv.getGUI().getX(),
            (cv.getGUI().getHeight() / 2) - getHeight() / 2 + cv.getGUI().getY());
    setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == okBtn_) {
      setVisible(false);
    } else if (e.getSource() == cancelBtn_) {
      cancelled_ = true;
      setVisible(false);
    }
  }

  private Route getSelectedRoute() {
    return routeLookup_[routesCBox_.getSelectedIndex()];
  }

  private void updateRoutes(Provider p, int selRteIndex) {
    routesCBox_.removeAllItems();
    routeLookup_ = new Route[routes_.routeCount(p)];
    int i = 0;
    for(Route route : routes_.getCollectionSortedByID(p)) {
      routeLookup_[i] = route;
      routesCBox_.addItem(RouteSelectionPanel.getRouteDisplayStr(route));
      i++;
    }
    routesCBox_.setSelectedIndex(selRteIndex);
    updateSubs();
  }

  public void updateSubs() {
    subsCBox_.removeAllItems();
    if (routesCBox_.getSelectedIndex() == -1 || getSelectedRoute().subRouteCount() == 0) {
      subsCBox_.setEnabled(false);
      return;
    }
    subsCBox_.setEnabled(true);
    for(SubRoute sub : getSelectedRoute().getSubRoutes()) {
      subsCBox_.addItem(sub.getID());
    }
  }

  public SubRoute getSelectedSubRoute() {
    if (cancelled_) {
      return null;
    }
    return getSelectedRoute().getSubRoute((String) subsCBox_.getSelectedItem());
  }

  public boolean getReverse() {
    return reverseCB_.isSelected();
  }
}























